package com.bryant.miantong.ui.login

import android.app.ActivityOptions
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.Toast
import androidx.lifecycle.lifecycleScope

import com.bryant.bryant_weather.base.BaseBindingActivity
import com.bryant.miantong.MainActivity
import com.bryant.miantong.R
import com.bryant.miantong.bean.LoginRequest
import com.bryant.miantong.databinding.ActivityLoginBinding
import com.bryant.miantong.utils.MMKVManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class LoginActivity : BaseBindingActivity<ActivityLoginBinding>() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        检查是否已经登录
        if (MMKVManager.isLoggedIn()) {
            Toast.makeText(this, "已登录，请勿重复登录", Toast.LENGTH_SHORT).show()
            startActivity(Intent(this, MainActivity::class.java))
            finish()
            return // 添加return避免继续执行下面的代码
        }
        // 设置默认账号和密码
        binding.etUsername.setText("hmheima")
        binding.etPassword.setText("Hmheima%123")

        binding.btnLogin.setOnClickListener {
            toLogin()
        }
    }


    override fun initBinding(layoutInflater: LayoutInflater): ActivityLoginBinding {
        return ActivityLoginBinding.inflate(layoutInflater)
    }

//    @Suppress("DEPRECATION")
//    override fun finish() {
//        super.finish()
//        // 使用 ActivityOptions 处理返回动画
//        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out)
//    }

    private fun toLogin() {
        // 1. 获取用户输入的账户名和密码
        val username = binding.etUsername.text.toString()
        val password = binding.etPassword.text.toString()
        // 2. 判断账户密码是否为空
        if (username.isEmpty() || password.isEmpty()) {
            Toast.makeText(this, "请输入账号和密码", Toast.LENGTH_SHORT).show()
            return
        }
        // 3. 登录逻辑
        try {
            lifecycleScope.launch {
                // 1. 创建登录请求对象，传入用户输入的账户名和密码
                val username = binding.etUsername.text.toString()
                val password = binding.etPassword.text.toString()

//                val loginRequest = LoginRequest("hmheima", "Hmheima%123")
                val loginRequest = LoginRequest(username, password )

                // 2. 在IO线程中发送请求登录
                val res = withContext(Dispatchers.IO) {
                    RetrofitClient.apiService.login(loginRequest)
                }
                if (res.code == 10000) {
                   Toast.makeText(this@LoginActivity, "登录成功${res}", Toast.LENGTH_SHORT).show()
                    val token = res.data?.token
                    if (token != null) {
                        MMKVManager.saveLoginInfo(res.data?.token ?: "")
                        // 4. 跳转到主页
                        startActivity(Intent(this@LoginActivity, MainActivity::class.java))
                        finish()
                    }

                } else {
                    // 登录失败
                    Toast.makeText(this@LoginActivity, "登录失败: ${res.message}", Toast.LENGTH_SHORT).show()
                }

            }
        } catch (e: Exception) {
            Toast.makeText(this, "登录失败: ${e.message}", Toast.LENGTH_SHORT).show()
        }
    }


}